__author__ = 'henry.woo@outlook.com'


fpath1="c:\\Users\\Public\\_tt\\_data\\henry\\ATHM-BITA.lst"
with open(fpath1) as f:
    s=f.readlines()

    validrange=[0,0]
    for i in s:
        (t,master,mbid,mask,slave,sbid,sask) = i.split(',')
        bid=mask-sbid
        ask=mbid-sask
        if (bid)

def draw(fpath, times):
    print fpath
    print times
    BINUM=200
    header_row=['dt','master','mbid','mask','slave','sbid', 'sask']
    ptotal=pd.read_csv(fpath,index_col=0,parse_dates=True,dayfirst=True, names=header_row)
    ptotal['bid']   = ptotal['mask'] - ptotal['sbid']
    ptotal['ask']   = ptotal['mbid'] - ptotal['sask']
    ptotal['spread']= ptotal['ask'] - ptotal['bid']

    for day in times:
        qstr="('{prev}'<=dt) & (dt<='{curr}')".format(prev=day + ' 09:29:59',
                                                      curr=day + ' 16:00:59')
        print qstr
        p1 = ptotal.query(qstr)
        if len(p1)==0:
          continue

        c=p1.spread
        ax1 = plt.subplot(221)
        symbol=p1['master'][0]+'-'+p1['slave'][0]
        pictime=str(p1.index[0]) + '~' + str(p1.index[-1])
        plt.title("spread of "+symbol + '@' + pictime)
        plt.setp(ax1.get_xticklabels(), visible=True)
        c.hist(bins=np.arange(c.min(),c.max(),(c.max()-c.min())/BINUM),color='g',alpha=0.4)

        ax2 = plt.subplot(222)
        plt.setp(ax1.get_xticklabels(), visible=True)
        p1.spread.plot(ax=ax2,style='o-')

        ax3 = plt.subplot(223)
        plt.title("ask(red),bid(blue)")
        plt.setp(ax3.get_xticklabels(), visible=True)
        c=p1.ask
        c.hist(bins=np.arange(c.min(),c.max(),(c.max()-c.min())/BINUM),color='r',alpha=0.4)
        plt.setp(ax3.get_xticklabels(), visible=True)
        c=p1.bid
        c.hist(bins=np.arange(c.min(),c.max(),(c.max()-c.min())/BINUM),color='b',alpha=0.4)

        ax4 = plt.subplot(224)
        plt.setp(ax4.get_xticklabels(), visible=True)
        p1.bid.plot(ax=ax4,style='o-')
        p1.ask.plot(ax=ax4,style='o-')
        #plt.subplots_adjust(left=0.1, bottom=0.1, wspace=0.09,hspace=0.09)

        plt.show()